package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.AdvertSettingDao;
import cn.yunyichina.provider.base.entity.AdvertSetting;
import cn.yunyichina.provider.base.vo.AddAdvertSettingRequestVo;
import cn.yunyichina.provider.base.vo.UpdateAdvertSettingRequestVo;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.page.PageInfo;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("advertSettingServiceImpl")
public class AdvertSettingServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(AdvertSettingServiceImpl.class);
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private AdvertSettingDao advertSettingDao;
    
    public ResponseObject listAdvertSetting(String data){
        ResponseObject responseObject = new ResponseObject();

        try {
            JSONObject request = JSONObject.parseObject(data);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageNum(request.getInteger("page_num"));
            pageInfo.setPageSize(request.getInteger("page_size"));

            List<AdvertSetting> advertSetting = advertSettingDao.selectAdvertSettingByPage(request.getString("business_code"),pageInfo);
            Map<String ,Object> resultMap = new HashMap<>();
            resultMap.put("datas",advertSetting);
            AdvertSetting wheres = new AdvertSetting();
            wheres.setBusinessCode(request.getString("business_code"));
            long counts = advertSettingDao.selectCount(wheres);
            resultMap.put("counts",counts);
            responseObject.setResultCodeAndMessageAndResult("0","成功",resultMap);
        }catch (Exception e){
           logger.error("查询广告列表出错",e);
            responseObject.setResultCodeAndMessage("-1","查询广告列表出错");
       }
        return responseObject;
    }


    public ResponseObject saveAdvertSetting(String data){
        ResponseObject responseObject = new ResponseObject();

        try {
            AddAdvertSettingRequestVo requestVo = JsonUtils.parseObject(data, AddAdvertSettingRequestVo.class);
            AdvertSetting dbAdvertSetting = advertSettingDao.selectByAdName(requestVo.getAdName());
            if (dbAdvertSetting != null){
                responseObject.setResultCodeAndMessage("1002","广告名称要求唯一");
                return responseObject;
            }
            AdvertSetting advertSetting = new AdvertSetting();
            advertSetting.setId(PKGenerator.generateId());
            advertSetting.setAdName(requestVo.getAdName());
            advertSetting.setAdCode(requestVo.getAdCode());
            advertSetting.setBusinessCode(requestVo.getBusinessCode());
            advertSetting.setPlatformCode("BONE");
            advertSetting.setLinkUrl(requestVo.getLinkUrl());
            advertSetting.setPicUrl(requestVo.getPicUrl());
            advertSetting.setShowOrders(requestVo.getShowOrders());
            advertSetting.setIsShow(requestVo.getIsShow());
            advertSetting.setCreatedTime(simpleDateFormat.format(new Date()));

            advertSettingDao.insert(advertSetting);
            responseObject.setResultCodeAndMessage("0","成功");
        }catch (Exception e){
            logger.error("新增广告配置异常");
            responseObject.setResultCodeAndMessage("-1","新增广告配置异常");
        }
        return responseObject;
    }

    public ResponseObject deleteAdvertSetting(String data) {
        ResponseObject responseObject = new ResponseObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            if (StringUtils.isBlank(jsonObject.getString("id"))){
                responseObject.setResultCodeAndMessage("1001","id 不能为空");
                return responseObject;
            }
            AdvertSetting advertSetting = new AdvertSetting();
            advertSetting.setId(jsonObject.getString("id"));
            advertSettingDao.delete(advertSetting);
            responseObject.setResultCodeAndMessage("0","成功");
        }catch (Exception e){
            logger.error("删除广告配置出错",e);
            responseObject.setResultCodeAndMessage("-1","删除广告配置出错");
        }
        return responseObject;
    }

    public ResponseObject updateAdvertSetting(String data) {
        ResponseObject responseObject = new ResponseObject();

        try{
            UpdateAdvertSettingRequestVo requestVo = JsonUtils.parseObject(data, UpdateAdvertSettingRequestVo.class);
            AdvertSetting dbAdvertSetting = advertSettingDao.selectByAdName(requestVo.getAdName());
            if (dbAdvertSetting != null && !dbAdvertSetting.getId().equals(requestVo.getId()) ){
                responseObject.setResultCodeAndMessage("1002","广告名称要求唯一");
            }
            AdvertSetting advertSetting = new AdvertSetting();
            advertSetting.setId(requestVo.getId());
            advertSetting.setAdName(requestVo.getAdName());
            advertSetting.setAdCode(requestVo.getAdCode());
            advertSetting.setPlatformCode("BONE");
            advertSetting.setBusinessCode(requestVo.getBusinessCode());
            advertSetting.setLinkUrl(requestVo.getLinkUrl());
            advertSetting.setPicUrl(requestVo.getPicUrl());
            advertSetting.setShowOrders(requestVo.getShowOrders());
            advertSetting.setIsShow(requestVo.getIsShow());
            advertSettingDao.update(advertSetting);
            responseObject.setResultCodeAndMessage("0","成功");
        }catch (Exception e){
            logger.error("更新广告配置出错",e);
            responseObject.setResultCodeAndMessage("-1","更新广告配置出错");
        }
        return responseObject;
    }



}
